package com.ruoyi.wuchuan.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.wuchuan.domain.EcoWorkTask;
import com.ruoyi.wuchuan.domain.ResultData;
import com.ruoyi.wuchuan.domain.WorkTask;
import com.ruoyi.wuchuan.mapper.HomeworkTaskMapper;
import com.ruoyi.wuchuan.service.HomeworkTaskService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * 作业任务
 */
@Service("homeworkTaskService")
@Transactional(rollbackFor = Exception.class)
public class HomeworkTaskServiceImpl implements HomeworkTaskService {

    @Resource
    private HomeworkTaskMapper homeworkTaskMapper;
    /**
     * 添加作业任务
     * @param workTask
     * @return
     */
    public ResultData addWorkTask(WorkTask workTask){
        ResultData resultData = new ResultData();
        try{
            String userName =  SecurityUtils.getUsername();
            if(StringUtils.isEmpty(workTask.getId()) && StringUtils.isEmpty(workTask.getCreateUser())) {
                workTask.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                workTask.setCreateUser(userName);
                if(homeworkTaskMapper.repeatByName(workTask)>0){
                    resultData.setSuccess(false);
                    resultData.setMessage("新建作业任务名称重复，请修改在保存！");
                    return resultData;
                }else{
                    if (homeworkTaskMapper.addWorkTask(workTask) > -1) {
                        List<EcoWorkTask> ewts = workTask.getEcoWorkTasks();
                        if (ewts != null && ewts.size() > 0) {
                            for (EcoWorkTask ewt : ewts) {
                                ewt.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                                ewt.setTaskId(workTask.getId());
                                homeworkTaskMapper.addEcoWorkTask(ewt);
                            }
                        }
                    }
                }
            }
            return resultData;
        }catch (Exception e){
            e.printStackTrace();
            resultData.setSuccess(false);
            resultData.setMessage("新建作业任务异常，异常原因："+e.getMessage());
            return resultData;
        }
    }

    /**
     * 删除作业任务
     * @param workTask
     * @return
     */
    public Boolean delWorkTask(WorkTask workTask){
        try{
            if(homeworkTaskMapper.delWorkTask(workTask) > -1 && homeworkTaskMapper.delEcoWorkTask(workTask) > -1){
                return true;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 修改作业任务
     * @param workTask
     * @return
     */
    public Boolean editWorkTask(WorkTask workTask){

        if(StringUtils.isEmpty(workTask.getId())){
            return false;
        }
//        if(homeworkTaskMapper.editWorkTask(workTask) > -1 && homeworkTaskMapper.delEcoWorkTask(workTask) > -1 && homeworkTaskMapper.addEcoWorkTask(workTask)  > -1){
//            return true;
//        }
        return false;
    }

    /**
     * 根据名称模糊查询作业任务
     * @param workTask
     * @return
     */
    public List<WorkTask> queryWorkTaskByName(WorkTask workTask){
        List <WorkTask> list = null;
        try{
            String userName =  SecurityUtils.getUsername();
            workTask.setCreateUser(userName);
            list = homeworkTaskMapper.queryWorkTaskByName(workTask);
            if(list !=null && list.size()>0){
                for (WorkTask w : list) {
                    List<EcoWorkTask> ecos = homeworkTaskMapper.queryEcoWorkTaskByTaskId(w.getId());
                    w.setEcoWorkTasks(ecos);
                }
            }
            return list;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
